package com.manage.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class BugMainActivity extends ActivityGroup {
	private static ViewGroup container;
	private static BugMainActivity thiz;
	private static Button btnCalDate, btnHealthNotice, btnPostQuestion, btnSetting;
	private static Map<Integer, View> subViews = new HashMap<Integer, View>();
	protected static boolean isPasswordValidated = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setContentView(R.layout.bug_main_page);
		BugMainActivity.subViews.clear();
		BugMainActivity.thiz = this;
		this.init();
	}

	private void init() {
		container = (ViewGroup) this.findViewById(R.id.container);
		btnCalDate = (Button) this.findViewById(R.id.buttonCalDate);
		btnHealthNotice = (Button) this.findViewById(R.id.buttonHealthNotice);
		btnPostQuestion = (Button) this.findViewById(R.id.buttonPostQuestion);
		btnSetting = (Button) this.findViewById(R.id.buttonSetting);
		NavigationButtonsOnClickListener myOnClickListener = new NavigationButtonsOnClickListener();
		btnCalDate.setOnClickListener(myOnClickListener);
		btnHealthNotice.setOnClickListener(myOnClickListener);
		btnPostQuestion.setOnClickListener(myOnClickListener);
		btnSetting.setOnClickListener(myOnClickListener);
	}

	private class NavigationButtonsOnClickListener implements View.OnClickListener {

		public void onClick(View v) {
			resetNavigationBarButtons();
			switch (v.getId()) {
			case R.id.buttonCalDate:
				btnCalDate.setBackgroundResource(R.drawable.btn_cal_date_down);
				switchActivity(BugIndexActivity.class);
				break;
			case R.id.buttonHealthNotice:
				btnHealthNotice.setBackgroundResource(R.drawable.btn_health_info_down);
				switchActivity(AboutActivity.class);
				break;
			case R.id.buttonPostQuestion:
				btnPostQuestion.setBackgroundResource(R.drawable.btn_question_online_down);
				switchActivity(SinaShareActivity.class);
				break;
			case R.id.buttonSetting:
				btnSetting.setBackgroundResource(R.drawable.btn_settings_down);
				switchActivity(SettingActivity.class);
			}
		}
	}

	private static void resetNavigationBarButtons() {
		btnCalDate.setBackgroundResource(R.drawable.btn_cal_date);
		btnHealthNotice.setBackgroundResource(R.drawable.btn_health_info);
		btnPostQuestion.setBackgroundResource(R.drawable.btn_question_online);
		btnSetting.setBackgroundResource(R.drawable.btn_settings);
	}

	// 请求缓存中的view，如果这个view没有父view，则添加到container中，否则返回false并将该view从缓存中清除。
	private static boolean useCachedActivity(Class<?> c) {
		if (isActivityShown(c)) {
			return true;
		}
		container.removeAllViews();
		container.startAnimation(AnimationUtils.loadAnimation(thiz, android.R.anim.fade_in));
		View view = subViews.get(c.hashCode());
		if (view == null) {
			return false;
		}
		ViewParent parent = view.getParent();
		if (parent == null) {
			container.removeAllViews();
			container.addView(view);
			return true;
		}
		subViews.remove(c.hashCode());
		return false;
	}

	private static boolean isActivityShown(Class<?> c) {
		View child = container.getChildAt(0);
		if (child != null && c.hashCode() == child.getId()) {
			return true;
		}
		return false;
	}

	private static void createAndAddActivityToContainer(Class<?> c, Intent intent) {
		if (intent == null) {
			intent = new Intent(thiz, c);
		} else {
			intent.setClass(thiz, c);
		}
		int activityKey = c.hashCode();
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View v = thiz.getLocalActivityManager().startActivity(String.valueOf(activityKey), intent).getDecorView();
		v.setId(activityKey);
		subViews.remove(activityKey);
		subViews.put(activityKey, v);
		container.removeAllViews();
		container.addView(v);
	}

	/**
	 * 跳转到相应的activity。使用缓存。
	 * 
	 * @param c
	 *            Actvity.class
	 */
	public static void switchActivity(Class<?> c) {
		switchActivity(c, false, null);
	}

	/**
	 * 跳转到相应的activity。自定义是否使用缓存。
	 * 
	 * @param c
	 *            Actvity.class
	 * @param reload
	 *            是否使用缓存
	 */
	public static void switchActivity(Class<?> c, boolean reload) {
		switchActivity(c, reload, null);
	}

	/**
	 * 跳转到相应的activity，并提供intent。使用缓存。
	 * 
	 * @param c
	 *            Actvity.class
	 * @param intent
	 */
	public static void switchActivity(Class<?> c, Intent intent) {
		switchActivity(c, false, intent);
	}

	/**
	 * 跳转到相应的activity。自定义是否使用缓存。自定义intent，intent可为空。
	 * 
	 * @param c
	 *            Actvity.class
	 * @param reload
	 * @param intent
	 */
	public static void switchActivity(Class<?> c, boolean reload, Intent intent) {
		// 如果使用缓存成功，则直接返回。
		if (!reload && useCachedActivity(c)) {
			return;
		}
		createAndAddActivityToContainer(c, intent);
	}

	protected void showExitDialog() {
		AlertDialog.Builder builder = new Builder(thiz);
		builder.setMessage(thiz.getResources().getString(R.string.exit_comfirm));
		builder.setPositiveButton(thiz.getResources().getString(R.string.button_ok),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						isPasswordValidated = false;
						thiz.finish();
					}
				});
		builder.setNegativeButton(thiz.getResources().getString(R.string.button_cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	/**
	 * 监听返回建，提示用户是否退出程序。
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showExitDialog();
			return false;
		}
		return false;
	}

}
